<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
if (!function_exists('formatEncode')) {
    /**
     * 加密设备号
     * @return string
     */
    function formatEncode($code)
    {
       $str = base64_encode('LIFE'.sprintf('%08X',$code));

       return $str.substr(md5($str),8,16);
    }
}
if (!function_exists('formatDecode')) {
    /**
     * 解密设备号
     * @return string
     */
    function formatDecode($code)
    {
        try {
            $encrypt = substr($code,-16);
            $encode = str_replace($encrypt,'',$code);
            if(substr(md5($encode),8,16) != $encrypt){
                return false;
            }
            return base_convert(str_replace('LIFE','',base64_decode($encode)),16,10);
        } catch (\Throwable $th) {
        	return false;
        }
       
    }
}
if (!function_exists('formatDechex')) {
    /**
     * 格式化16进制
     * @return string
     */
    function formatDechex($code)
    {
       $hex = dechex($code);
       if(strlen($hex)%2 !=0){
        $hex = '0'.$hex;
       }
      return strtoupper($hex);
    }
}
if (!function_exists('uuid')) {
    /**
     * 获取全球唯一标识
     * @return string
     */
    function uuid()
    {
        return sprintf(
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff)
        );
    }
}

if (!function_exists('rmdirs')) {

    /**
     * 删除文件夹
     * @param string $dirname  目录
     * @param bool   $withself 是否删除自身
     * @return boolean
     */
    function rmdirs($dirname, $withself = true)
    {
        if (!is_dir($dirname)) {
            return false;
        }
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileinfo) {
            $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
            $todo($fileinfo->getRealPath());
        }
        if ($withself) {
            @rmdir($dirname);
        }
        return true;
    }
}
    /**
     * 将层级数组遍历成一维数组
     * @param array $list
     * @param int $lv
     * @param string $title
     * @return array
     */
function formatTree(array $list, int $lv = 0, string $title = 'title'){
    $formatTree = [];
    foreach ($list as $key => $val) {
        $title_prefix = '';
        for ($i = 0; $i < $lv; $i++) {
    
            $title_prefix .= "&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        $title_prefix .= "|---";
        $val['lv'] = $lv;
        $val['namePrefix'] = $lv == 0 ? '' : $title_prefix;
        $val['showName'] = $lv == 0 ? $val[$title] : $title_prefix . $val[$title];
        if (!array_key_exists('children', $val)) {
            array_push($formatTree, $val);
        } else {
            $child = $val['children'];
            unset($val['children']);
            array_push($formatTree, $val);
            $middle = formatTree($child, $lv + 1, $title); //进行下一层递归
            $formatTree = array_merge($formatTree, $middle);
        }
    }

    return $formatTree;
}
    /**
     * 把返回的数据集转换成Tree
     * @param $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param string $root
     * @return array
     */
function listToTree( $list, $pk = 'id', $pid = 'pid', $child = 'children', $root = '0'){
        $tree = [];
        if (is_array($list)) {
            $refer = [];
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = &$list[$key];
            }
            foreach ($list as $key => $data) {
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] = &$list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = &$refer[$parentId];
                        $parent[$child][] = &$list[$key];
                    }
                }
            }
        }
        return $tree;
    }
